<?php

namespace pc\models;

use common\Service\JiebaService;
use common\Service\RedisService;
use Yii;
use common\models\Star as P_Star;

class Star extends P_Star
{
    /**
     * 初始化明星数据 正式明星数据放入redis
     */
    public function setStarRedis(){
        ini_set("max_execution_time",0);
        ini_set("memory_limit", "1024M");
        $stars = Star::find()->where('status = 1')->orderBy('id desc')->limit(2000)->asArray()->all();

//        $jieba = new JiebaService();
        foreach($stars as  $star){
//            $star['view'] = rand(10000,20000);
//            $star['tags'] = implode(',',array_keys($jieba->extractTags($star['desc'],50)));
            RedisService::setHash('star:'.$star['id'],$star);
            Yii::$app->get('redis')->zadd('starZset',intval($star['view']),intval($star['id']));
            $model = Star::findOne($star['id']);
            $model->setAttribute('status',3);
//            $model->setAttribute('view',$star['view']);
//            $model->setAttribute('tags',$star['tags']);
            $model->save();
        }
        exit('ok');
    }


    /*
     * 分析明星数据
     * step1:职业job 国家country  出生地 birth_place 星座star_sign 生肖animal
     */
    public function analysisStar(){
        ini_set("max_execution_time",0);
        ini_set("memory_limit", "1024M");
        //24311
        $redis = Yii::$app->redis;

        $startId = (int)$redis->get('tagStartId') ?: 16667;
        $startId = 16667;
        $count = 0;
        for($i = $startId ; $i < 24311 ; $i++){
            $starInfo = RedisService::getHash('star:'.$i);

            //职业维度
//            $job = isset($starInfo['job']) ? $starInfo['job'] : '';
//            $this->jobAnalysis($i,$job,$redis);

            //国家地区维度
//            $country = isset($starInfo['country']) ? $starInfo['country'] : '';
//            $this->countryAnalysis($i,$country,$redis);
//            //标签 萝莉 美少女 御姐 熟女 美腿 美臀 平胸 妹纸 骨感美女 童颜巨乳 波涛汹涌 人间胸器 G奶女神
//
//            if(isset($starInfo['tags'])){
//                $tags = explode(',',$starInfo['tags']);
//                foreach ($tags as $tag){
//                    $redis->ZIncrby('M_TagSSet',1,$tag);
//                }
//            }
//            $redis->set('tagStartId',$i);
//            $count++;
//            if($count > 500) break;

        }

    }

    public function jobAnalysis($starId = 0,$job = '',$redis){
            //职业维度
            if(!empty($job)){
                $job = explode(',',str_replace(['、',',','，',' ',' ','。'],',',$job));
                foreach($job as $_job){
                    $_job = strtolower($_job);

                    if(strpos($_job,'cos') !== false) $_job = 'cosplayer';
                    if(strpos($_job,'show') !== false || strpos($_job,'shwo') !== false ) $_job = 'showgirl';

                    $redisKey = trim($_job);
                    if(empty($_job) || $_job == '不详'){
                        $redisKey = 'OtherJob';
                    }

                    $redis->ZIncrby('M_JobSSet',1,$redisKey);
                    $view = RedisService::getHash('star:'.$starId,['view'])['view'];
                    $redis->ZAdd('job:'.trim($redisKey),intval($view),$starId);

                }
            }
    }

    public function countryAnalysis($starId = 0,$country = '',$redis)
    {
        //国家地区维度
        if (strpos($country, '中国') !== false || strpos($country, '中华') !== false || $country == 'zhongg' || $country == 'zg') $country = '中国';
        $redisKey = trim($country);
        if (empty($country) || $country == '不详') {
            $redisKey = 'OtherCountry';
        }

        $redis->ZIncrby('M_CountrySSet', 1, $redisKey);
        $view = RedisService::getHash('star:' . $starId, ['view'])['view'];
        $redis->ZAdd('country:' . $redisKey, intval($view), $starId);
    }

    /**
     * 設置明星名字HASH key:名字、昵称 value：starId
     */
    public function setStarNameToIdHash(){
        $redis = Yii::$app->get('redis');
        $starId = (int)$redis->get('starId');

        $sqlCommand = "SELECT id, CONCAT(`name`,',',nick_name) 'name' FROM {{%star}} WHERE id > {$starId} limit 3000";
        $stars = Yii::$app->db->createCommand($sqlCommand)->queryAll();
        if(empty($stars)) echo 'ok';

        foreach ($stars as $star){
            $star['name'] = explode(',',$star['name']);
            if(!empty($star['name'])){
                foreach ($star['name'] as $name){
                    if(!empty($name)) $redis->hset('starNameIds',trim($name),intval($star['id']));
                }
            }
            $redis->set('starId',intval($star['id']));
        }
        echo 'ok';
    }


}
